package cn.edu.jxust.core.config;

import cn.edu.jxust.application.filter.JwtFilter;
import cn.edu.jxust.core.infrastructure.util.authc.JwtUtils;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author QiuKangming
 * @version 1.0
 * @description Shrio 的配置类
 * @since 2020/06/15 10:48
 */

@Configuration
public class ShiroConfig {

    /**
     * 开启权限注解支持
     * @return DefaultAdvisorAutoProxyCreator
     */
    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {

        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        // 强制使用cglib，防止重复代理和可能引起代理出错的问题
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;

    }

    /**
     * 开启权限注解支持
     * @return LifecycleBeanPostProcessor
     */
    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {

        return new LifecycleBeanPostProcessor();

    }

    /**
     * 将自定义Bean放入 IOC 容器
     * @return ShiroRealm
     */
    @Bean
    public ShiroRealm shiroRealm(){

        return new ShiroRealm();

    }

    /**
     * @param securityManager 安全管理器
     * @return AuthorizationAttributeSourceAdvisor
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {

        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;

    }

    /**
     * 创建Shiro的安全管理器
     * @param shiroRealm 自定义realm
     * @return DefaultWebSecurityManager
     */
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager defaultWebSecurityManager(ShiroRealm shiroRealm/*,@Qualifier("cookieRememberMeManager") CookieRememberMeManager cookieRememberMeManager*/){

        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();

        //关闭shiro自带的session
        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
        subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        securityManager.setSubjectDAO(subjectDAO);
        //屏蔽设置 Cookie 管理器
        securityManager.setRememberMeManager(null);
        //关联 realm
        securityManager.setRealm(shiroRealm);
        return securityManager;

    }


    /**
     * 自定义Shiro的过滤链配置, 用于读取 yaml 配置类中的过滤链
     * @return ShiroFilterProperties
     */
    @Bean
    public ShiroFilterProperties shiroFilterProperties(){

        return new ShiroFilterProperties();

    }

    /**
     * 创建 Shiro FilterFactoryBean
     * @param securityManager 安全管理器
     * @param jwtUtils jwt 工具类
     * @return ShiroFilterFactoryBean
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager, JwtUtils jwtUtils){

        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        shiroFilterFactoryBean.setLoginUrl("/needLogin");
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthc");

        //添加 jwt 过滤器
        Map<String, Filter> filters = new HashMap<>();
        filters.put("jwt", new JwtFilter(jwtUtils));
        shiroFilterFactoryBean.setFilters(filters);

        //动态配置拦截器注入
        Map<String, String> filterMap = new LinkedHashMap<>(5);
        List<Map<String, String>> perms = this.shiroFilterProperties().getPerms();
        perms.forEach(perm -> filterMap.put(perm.get("key"), perm.get("value")));

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactoryBean;

    }

}
